<?php

class CW_Debug {

	private $Reg;

	private $buffer_xml;
	private $buffer_xsl;
	private $buffer_html;

	public function __construct(&$Reg)
	{
		$this->Reg =& $Reg;

		$this->buffer_xml  = '';
		$this->buffer_xsl  = '';
		$this->buffer_html = '';
	}

	public function setXml($xml = '')
	{
		$this->buffer_xml = trim($xml);

		return true;
	}

	public function setXsl($xsl = '')
	{
		$this->buffer_xsl = trim($xsl);

		return true;
	}

	public function setHtml($output = '')
	{
		$this->buffer_html = trim($output);

		return true;
	}

	public function showDebugScreen($screen = 'xml')
	{
		$kickback = '?' . preg_replace('#(&)debug(=xml|=xsl|=html|=queries)#i', '', $_SERVER['QUERY_STRING']);

		$output  = "<html>\n";
		$output .= "\t<head>\n";
		$output .= "\t\t<title>Debug Mode</title>\n";
		$output .= "\t\t<style type=\"text/css\">\n";
		$output .= "\t\t\tbody { background: #333; color: #fff; padding: 25px; font-family: Arial, sans-serif; }\n";
		$output .= "\t\t\tbody, div, h1, h2, h3, h4, p { font-family: Arial, verdana;}\n";
		$output .= "\t\t\th1 { font-size: 2.2em; }\n";
		$output .= "\t\t\th2 { color: #c00; font-size: 1em; }\n";
		$output .= "\t\t\tli { color: #666; font-size: .8em; padding: 3px 0;}\n";
		$output .= "\t\t\tcode { color: #ddd; font-family: courier;}\n";
		$output .= "\t\t\ta:link, a:visited { color: #fff; text-decoration: none; margin-left: 10px; }\n";
		$output .= "\t\t\ta:hover { color: #ddd; text-decoration: none; }\n";
		$output .= "\t\t\tspan { color: #666; margin-left: 10px; font-size: 1em; }\n";
		$output .= "\t\t</style>\n";
		$output .= "\t<body>\n";

		$output .= '<span style="float: right;"><a href="' . str_replace('&debug', '', $kickback) . '" title="">x</a></span>';
		$output .= "<strong>Debug Mode:</strong>";

		$screen_types = array('xml', 'xsl', 'html');


		foreach($screen_types as $type)
		{
			if($screen == $type)
			{
				$output .= "<span>" . ucwords($type) . "</span>";
			}
			else {
				$output .= "<a href=\"{$kickback}&amp;debug={$type}\" title=\"\">" . ucwords($type) . '</a>';
			}
		}

		$buffer = '<br /><br />';

		if(false == class_exists('CW_Common'))
		{
			$buffer = '<p>Error: Required library "Common" does not exist.</p>';
		}
		else {
			switch($screen)
			{
				case 'xml':

					$buffer .= CW_Common::returnLineNumbers($this->buffer_xml);

					break;

				case 'xsl':

					$buffer .= CW_Common::returnLineNumbers($this->buffer_xsl);

					break;

				case 'html':

					$buffer .= CW_Common::returnLineNumbers($this->buffer_html);

					break;

				default:

					$buffer .= '<p>Error: Unknown Command</p>';

					break;
			}
		}

		$output .= $buffer;
		$output .= "\t</body>";
		$output .= "</html>";

		return $output;
	}
}

?>